Skip to content

fix(config): give headless default values the field's type - #157

Merged
clansty merged 2 commits into
MuNET-OSS:mainfrom
Cleroth:fix/config-default-type
Oct 9, 2026
Merged

clansty merged 2 commits into
MuNET-OSS:mainfrom
Cleroth:fix/config-default-type

Conversation

@Cleroth

@Cleroth Cleroth commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

MonoCecilReflectionProvider (used by the headless loader and the build tools) reads a field's default value from the IL constant in .cctor. IL pushes integer constants as int32, or int64 after conv.i8, whatever the field's type. So byte, ushort and uint entries got int defaults.

This breaks any consumer that compares a typed value with the default. For example, MaiChartManager's config save treats (ushort)32 as different from (int)32, so it writes every such entry out uncommented as if the user had changed it:

[GameSystem.Sound]
BitsPerSample = 32

The fix casts integer constants to the field's primitive type. The cast is unchecked, because the constant holds the field's bits: for example, uint 3000000000 is pushed as -1294967296. bool keeps its existing path, and non-integer constants are unchanged.

Verification

I ran MonoCecilReflectionProvider on the upstream AquaMai.Mods.dll, before and after, counting [ConfigEntry] fields of primitive type whose default value's runtime type differs from the field type:

Primitive entries Default type ≠ field type
Before 196 38 (GameSystem.Sound BitsPerSample / ValidBitsPerSample / SamplesPerSec, GameSettings.CreditConfig.LockCredits, all 34 GameSettings.TouchSensitivity entries)
After 196 0

MonoCecilReflectionProvider(HeadlessLoader 与构建工具使用)从 .cctor 的 IL 常量读取字段默认值。IL 总是以 int32(conv.i8 后为 int64)压入整数常量,与字段类型无关,因此 byte / ushort / uint 配置项的默认值变成了 int。比较默认值与实际值的工具(例如 MaiChartManager 保存配置)会把这些项都当作“已修改”,并以未注释的形式写出(如 BitsPerSample = 32)。

修复:把整数常量转换为字段的基本类型(unchecked,常量即字段的位模式)。验证:对上游 AquaMai.Mods.dll 的 196 个基本类型配置项,默认值类型与字段类型不一致的数量从 38 降为 0。

View guided diff

Sourcery 摘要

错误修复:

  • 确保从 IL 加载的整数字段默认值保留字段的原始运行时类型,防止将类型化值错误地视为已修改。
Original summary in English

Sourcery 摘要

错误修复:

  • 保留从 IL 加载的整数字段默认值所声明的基本类型,避免将类型化配置值错误地视为已修改。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Preserve the declared primitive types of integer field defaults loaded from IL so typed configuration values are not incorrectly treated as modified.

The Mono.Cecil reflection provider read integer defaults straight from the IL constant, so byte, ushort and uint entries got int defaults. Editors that compare the default with a typed value (MaiChartManager's config save) saw every such entry as changed and wrote it out uncommented, e.g. GameSystem.Sound.BitsPerSample = 32. Defaults are now cast to the field type.
@sourcery-ai

sourcery-ai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
审阅者指南(小型 PR 中折叠显示)

审阅者指南

更新 headless/build-tool 反射,使整数支持字段的默认值以字段声明的原始类型返回,从而防止类型值比较错误地将未更改的配置项识别为已修改。

类型化配置默认值加载的时序图

sequenceDiagram
    participant Provider as MonoCecilReflectionProvider
    participant IL as .cctor IL
    participant Field as Config field
    participant Consumer as Config consumer

    Provider->>IL: 读取整数常量
    IL-->>Provider: int32 或 int64 位
    Provider->>Field: 获取类型代码(fieldType)
    Provider->>Provider: 将位转换为声明的原始类型
    Provider-->>Consumer: 类型化默认值
    Consumer->>Consumer: 将实际值与类型化默认值进行比较
Loading

修正整数默认类型的流程图

flowchart LR
    A[整数 IL 常量] --> B{字段是原始类型吗?}
    B -->|否| C[保留原始常量]
    B -->|是| D[将位转换为字段原始类型]
    D --> E[返回类型化默认值]
    C --> E
    E --> F[未更改的值不会被写入为已修改]
Loading

文件级变更

变更 详细信息 文件
在从 IL 常量解码整数型字段默认值时保留声明的原始类型。
  • 检测原始类型字段的 int32 和 int64 操作数。
  • 使用 unchecked 转换,将常量的位模式转换为对应的有符号或无符号整数 TypeCode。
  • 保留布尔值处理和非整数常量现有的处理路径。
AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs

提示和命令

与 Sourcery 交互

  • 触发新的审阅: 在拉取请求中评论 @sourcery-ai review。
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 回复审阅评论,请 Sourcery 根据该评论创建 issue。你也可以回复审阅评论并使用 @sourcery-ai issue,从该评论创建 issue。
  • 生成拉取请求标题: 在拉取请求标题的任意位置写入 @sourcery-ai,即可随时生成标题。你也可以在拉取请求中评论 @sourcery-ai title,以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任意位置写入 @sourcery-ai summary,即可在指定位置随时生成 PR 摘要。你也可以在拉取请求中评论 @sourcery-ai summary,以随时(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。如果你已经处理完所有评论且不想再看到它们,此功能会很有用。
  • 关闭所有 Sourcery 审阅: 在拉取请求中评论 @sourcery-ai dismiss,即可关闭所有现有的 Sourcery 审阅。如果你想从新的审阅开始,这一功能尤其有用——别忘了评论 @sourcery-ai review 来触发新的审阅!

自定义使用体验

访问你的控制面板以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、删除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates headless/build-tool reflection so integer-backed field defaults are returned with the field’s declared primitive type, preventing typed-value comparisons from falsely identifying unchanged configuration entries as modified.

Sequence diagram for typed configuration default loading

sequenceDiagram
    participant Provider as MonoCecilReflectionProvider
    participant IL as .cctor IL
    participant Field as Config field
    participant Consumer as Config consumer

    Provider->>IL: Read integer constant
    IL-->>Provider: int32 or int64 bits
    Provider->>Field: GetTypeCode(fieldType)
    Provider->>Provider: Cast bits to declared primitive type
    Provider-->>Consumer: Typed default value
    Consumer->>Consumer: Compare actual value with typed default
Loading

Flow diagram for correcting integer default types

flowchart LR
    A[Integer IL constant] --> B{Field is primitive?}
    B -->|No| C[Keep original constant]
    B -->|Yes| D[Convert bits to field primitive type]
    D --> E[Return typed default]
    C --> E
    E --> F[Unchanged values are not written as modified]
Loading

File-Level Changes

Change Details Files
Preserve the declared primitive type when decoding integer field defaults from IL constants.
  • Detect int32 and int64 operands for primitive fields.
  • Convert the constant's bit pattern to the corresponding signed or unsigned integral TypeCode using unchecked casts.
  • Keep boolean handling and non-integer constants on their existing paths.
AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嘿——我发现了 1 个问题

给 AI 代理的提示
请处理本次代码审查中的评论:

## 单独评论

### 评论 1
<location path="AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs" line_range="145-155" />
<code_context>
+                    defaultValue = Type.GetTypeCode(fieldType) switch
</code_context>
<issue_to_address>
**Char 默认值的类型不正确**

当 `[ConfigEntry]` `char` 字段使用整数常量初始化器时,`GetFieldDefaultValue` 会继续执行到 `loadOperand`,因此 `ReflectionField.Value`/`GetValue` 返回的是装箱后的 `int`,而 `FieldType` 是 `char`;类型化比较以及 `IReflectionProvider` 的无头模式或构建工具使用者会看到类型不匹配的默认值。

请在 switch 中处理 `TypeCode.Char`,将整数常量转换为 `char`。

另请检查 `AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs:156`。
</issue_to_address>

Sourcery 评估

等待批准。 请先处理 1 个发现的问题。

阻塞性发现:AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs:155


Sourcery 对开源项目免费——如果您喜欢我们的审查,请考虑分享给他人 ✨
Original comment in English

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs" line_range="145-155" />
<code_context>
+                    defaultValue = Type.GetTypeCode(fieldType) switch
</code_context>
<issue_to_address>
**Char defaults have wrong type**

When a `[ConfigEntry]` `char` field has an integer constant initializer, `GetFieldDefaultValue` falls through to `loadOperand`, so `ReflectionField.Value`/`GetValue` returns a boxed `int` while `FieldType` is `char`; typed comparisons and headless or build-tool consumers of `IReflectionProvider` see a mismatched default.

Handle `TypeCode.Char` in the switch by converting the integer constant to `char`.

Also at `AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs:156`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs:155


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 1 file

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread AquaMai.Config/Reflection/MonoCecilAssemblyReflectionProvider.cs
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@clansty

clansty commented Oct 9, 2026

Copy link
Copy Markdown
Member

草 这好像是我 mcm 注释里面存在了很久的未解之谜

@clansty
clansty merged commit c64077a into MuNET-OSS:main Oct 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants